package org.yxing.palpalgps.activity;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import org.yxing.palpalgps.AbstractActivity;
import org.yxing.palpalgps.R;
import org.yxing.palpalgps.http.AsyncRequest;
import org.yxing.palpalgps.http.AsyncRequest.FetchListener;
import org.yxing.palpalgps.oauth.OAuth;
import org.yxing.palpalgps.utils.Helper;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;

public class CreateGroupChatActivity extends AbstractActivity implements OnClickListener {
    
    private static final String TAG = CreateGroupChatActivity.class.getSimpleName();
    
    private static final int RESULT_SELECTED_FRIEND = 1;
    
    private static final String URL_GROUP_START = "groupchat/start";
    
    private ArrayList<String> selectedUids;
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.create_chat_group);
        setTitle(getString(R.string.create_group));
        
        findViewById(R.id.layoutSelectFriend).setOnClickListener(this);
        findViewById(R.id.buttonSave).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.layoutSelectFriend:
            startActivityForResult(new Intent(this, SelectFriendActivity.class), RESULT_SELECTED_FRIEND);
            break;
        case R.id.buttonSave:
            saveGroup();
            break;
        }
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == RESULT_SELECTED_FRIEND) {
                selectedUids = data.getStringArrayListExtra("uids");
            }
        }
    }
    
    public void saveGroup() {
        EditText editTextName = (EditText) findViewById(R.id.editTextName);
        editTextName.clearFocus();
        String name = editTextName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            displayToast(R.string.name_can_not_be_empty);
            return;
        }
        
        if (selectedUids == null || selectedUids.size() == 0) {
            displayToast(R.string.please_select_friend);
            return;
        }
        
        Iterator<String> iterator = selectedUids.iterator();
        StringBuilder builder = new StringBuilder();
        while (iterator.hasNext()) {
            builder.append(iterator.next());
        }
        
        List<BasicNameValuePair> params = OAuth.createAuthParams();
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("fuids", Helper.join(selectedUids, ",")));
        AsyncRequest ar = new AsyncRequest(this);
        ar.fetch(URL_GROUP_START, params, new FetchListener() {
            
            @Override
            public void onError(int code, String msg) {
                
            }
            
            @Override
            public void onComplete(JSONObject response) {
                setResult(RESULT_OK);
                finish();
            }
        });
        
    }
}
